import pygame, sys, os
from pygame.locals import *
from map import *
from ai import Ai
from driver import KeySignal

class Control(object):
    WINDOW = (640, 640)
    CAPTION = 'Mini Tank'
    def __init__(self):
        if sys.platform in ("win32", "win64"):
            os.environ["SDL_VIDEO_CENTERED"] = "1"
        pygame.init()
        self.__window = pygame.display.set_mode(Control.WINDOW)
        pygame.display.set_caption(Control.CAPTION)
        self.__clock = pygame.time.Clock()
        self.__level = 1
        self.__keys = [K_UP, K_DOWN, K_LEFT, K_RIGHT]
    
    def start(self):
        # program loop
        while 1:
            # init map
            lvlmap = Map(mapcfg[self.__level])
            # init ai
            ai = Ai()
            # init sprite and group
            render = pygame.sprite.RenderUpdates(*lvlmap.all)
            # game loop
            while 1:
                # keep fps
                self.__clock.tick(50)
                # draw window
                self.__window.fill((50, 50, 100))
                render.draw(self.__window)
                pygame.display.update()
                # sys
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
                # get event                        
                key = pygame.key.get_pressed()
                for keyname in self.__keys:
                    if key[keyname]:
                        for spr in lvlmap.player:
                            spr.dirver.send(KeySignal(keyname))
                # sprite update
                for spr in lvlmap.player:
                    spr.update()
    
    
    
    
    
    
    
    
    
    
    
    
    